#include <stdio.h>
#include <string.h>
#include "course.h"

int main()
{
    struct Course courses[MAXCOURSES];
    
    /*input from file*/
    FILE *fp;
    fp=fopen("course.bin","r");
    //no such file
    if(fp==NULL){
        printf("File not exist!\n");
        return 0;
    }
    //read data
    fread(courses,sizeof(struct Course),MAXCOURSES,fp);
    //close file
    fclose(fp);
    

    /*
    //test input
    strcpy(courses[0].code, "COMP3012");
    strcpy(courses[0].name, "Advanced Computer Vision");
    strcpy(courses[0].gradeitems[0].name, "Assignment");
    strcpy(courses[0].gradeitems[1].name, "Individual Project");
    strcpy(courses[0].gradeitems[2].name, "Final");
    for (int i = 0; i < 3; i++)
    {
        courses[0].gradeitems[i].percentage = 30;
        courses[0].gradeitems[i].fullmark = 100;
        for (int j = 0; j < MAXGRADES; j++)
        {
            courses[0].gradeitems[i].grades[j] = -1;
        }
    }
    courses[0].gradeitems[2].percentage = 40;
    */

    /*main menu*/
    while (1)
    {
        printf("\n============== Main Menu ==============\n");
        printf("\t1. Edit Course\n");
        printf("\t2. Edit Grade\n");
        printf("\t3. Expected Final Grade\n");
        printf("\t4. View Course\n");
        printf("\t5. Exit\n");
        printf("=======================================\n\n");

        printf("Enter a number and choose an choiceation:");
        int choice;
        scanf("%d", &choice);
        switch (choice)
        {
        case 1: // Edit Course
            EditCourse(courses);
            break;
        case 2: // Edit Grade
            EditGrade(courses);
            break;
        case 3: // Expected Final Grade
            ExpectedFinalGrade(courses);
            break;
        case 4: // View Course
            ViewCourses(courses);
            break;
        case 5: // Save and Exit
            FILE *fp;
            fp=fopen("course.bin","w");
            if(fp==NULL){
                printf("Failed to save file!\n");
                return 0;
            }
            fwrite(courses,sizeof(struct Course),MAXCOURSES,fp);
            fclose(fp);
            printf("Saved to file!\n");
            return 1;
        default:
            break;
        }
    }

    return 0;
}
